const chai = require("chai")
const assert = chai.assert
const should = chai.should
const expect = chai.expect 
const http = require('http')

const RPCURL = "127.0.0.1"
const RPCPORT = "10000"


function rpcsend(method, params) {
    return new Promise( (resolve, reject) => {
        body = {
            jsonrpc: "2.0",
            method: method,
            params: params,
            id :"1"
        }
        
        bodyString = JSON.stringify(body)
        // console.log(bodyString)
        let headers = {
            'Content-Type': 'application/json; charset=utf-8',
            //'Content-Length': bodyString.length,
        }
        var options = {
            host: RPCURL,
            port: RPCPORT,
            path: '/',
            method: 'POST',
            headers: headers
        };
    
        var req=http.request(options,function(res){
            res.setEncoding('utf-8');
        
            var responseString = '';
        
            res.on('data', function(data) {
                responseString += data;
            });
        
            res.on('end', function() {
                var resultObject = JSON.parse(responseString);
                resolve(resultObject)
            });
        
            req.on('error', function(e) {
                reject(e)
            });
        });
        req.write(bodyString);
        req.end();
    }) 
}

describe('webapi-users', function() { 

    this.timeout(1000 * 5) 
    
    let idkey = ""
    let mobile = "15061519075"
    let token = ""
    
    describe("# UserGetCaptcha", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserGetCaptcha", {})
            .then(res => {
                console.log(res)
                expect(res.error).to.equal(null)
                expect(res.result.idkey).to.be.a("string")
                expect(res.result.base64).to.be.a("string")
                idkey = res.result.idkey
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })

    describe("# UserGetSignupMobileCode", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserGetSignupMobileCode", {
                mobile: mobile,
                idkey: idkey,
                cap: "whatever"
            })
            .then(res => {
                console.log(res)    
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })

    describe("# UserSignupByMobileCodePass", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserSignupByMobileCodePass", {
                mobile: mobile,
                pass: "77e7c96a",
                door: "I use a back door here"
            })
            .then(res => {
                console.log(res)
                //expect(res.result).to.equal("success")
                //expect(res.error).to.equal(null)
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })

    describe("# UserSigninByMobilePass", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserSigninByMobilePass", {
                mobile: mobile,
                pass: "77e7c96a",
            })
            .then(res => {
                console.log(res)
                expect(res.result.token).to.be.a("string")
                expect(res.error).to.equal(null)
                token = res.result.token
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })


    let basic_info = {
        realname: "祝巫师",
        location: "北京西二旗",
        gender: "male", 
        birthdate: "123238219", 
        bloodtype: "B", 
        introduction: "这个人很懒，什么都没有写。。。"
       
    }
    describe("# UserUpdateBasicInfo", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserUpdateBasicInfo", {
                token: token, 
                realname: basic_info.realname,
                location: basic_info.location,
                gender: basic_info.gender, 
                birthdate: basic_info.birthdate, 
                bloodtype: basic_info.bloodtype, 
                introduction: basic_info.introduction
            })
            .then(res => {
                console.log(res)
                expect(res.result).to.equal("success")
                expect(res.error).to.equal(null)
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })

    describe("# UserGetBasicInfo", function() {
        it(`should return correctly`, function(done) {
            rpcsend("UserGetBasicInfo", {
                token: token, 
            })
            .then(res => {
                console.log(res)
                expect(JSON.stringify(res.result)).to.equal(JSON.stringify(basic_info))
                expect(res.error).to.equal(null)
                done()
            })
            .catch(err =>{
                throw err
            })
        })
    })

})